package bpo;

import entity.Room;
import entity.RoomLog;
import entity.User;
import util.EntityManagerUtil;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RoomLogBpo {

    public static List<Map<String,String>> getAllRoomLog(){
        EntityManager em = EntityManagerUtil.getEntityManager();
        String sql = null;
        sql = "select * from roomlog";
        Query query=em.createNativeQuery(sql, RoomLog.class);
        List<RoomLog> RoomLogs = query.getResultList();
        em.close();
        List<Map<String,String>> tt = new ArrayList<Map<String,String>>();
        Map<String,String> cc = null;
        for (RoomLog s : RoomLogs) {
            cc = new HashMap<String,String>();
            cc.put("rno", s.getRno());
            cc.put("oldcost", String.valueOf(s.getOldcost()));
            cc.put("newcost", String.valueOf(s.getNewcost()));
            cc.put("updatetime", String.valueOf(s.getUpdatetime()));
            cc.put("username", s.getUsername());
            tt.add(cc);
        }
        return tt;
    }

    public static List<Map<String,String>> searchListByfr(String fno, String rno){
        EntityManager em = EntityManagerUtil.getEntityManager();
        String sql = null;
        sql = "SELECT * FROM roomlog rl WHERE rl.rno like '" + fno + '%' + "' and rl.rno like '%"+rno+"%'";


        Query query=em.createNativeQuery(sql, RoomLog.class);
        List<RoomLog> RoomLogs = (List<RoomLog>)query.getResultList();
        em.close();
        List<Map<String,String>> tt = new ArrayList<Map<String,String>>();
        Map<String,String> cc = null;
        for (RoomLog s : RoomLogs) {
            cc = new HashMap<String,String>();
            cc.put("rno", s.getRno());
            cc.put("oldcost", String.valueOf(s.getOldcost()));
            cc.put("newcost", String.valueOf(s.getNewcost()));
            cc.put("updatetime", String.valueOf(s.getUpdatetime()));
            cc.put("username", s.getUsername());
            tt.add(cc);
        }
        return tt;
    }
}
